
import Foundation
import UIKit
//===================视图宽高===========================
let SCREEN_WIDTH = UIScreen.main.bounds.width
let SCREEN_HEIGHT = UIScreen.main.bounds.height
//===================颜色===========================
let kWhiteColor = UIColor.white
let kBlackColor = UIColor.black
let kRedColor = UIColor.red
let kClearColor = UIColor.clear
public func hexStringToColor(hexString: String) -> UIColor{
    return hexStringToColor(hexString: hexString, alpha: 1)
}
public func hexStringToColor(hexString: String,alpha:Float) -> UIColor{
    var cString: String = hexString.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
    
    if cString.count < 6 {
        return UIColor.black
    }
    if cString.hasPrefix("0X")||cString.hasPrefix("0x") {
        cString = cString.substring(from: cString.index(cString.startIndex, offsetBy: 2))
    }
    if cString.hasPrefix("#") {
        cString = cString.substring(from: cString.index(cString.startIndex, offsetBy: 1))
    }
    if cString.count != 6 {
        return UIColor.black
    }
    
    var range: NSRange = NSMakeRange(0, 2)
    let rString = (cString as NSString).substring(with: range)
    range.location = 2
    let gString = (cString as NSString).substring(with: range)
    range.location = 4
    let bString = (cString as NSString).substring(with: range)
    
    var r: UInt32 = 0x0
    var g: UInt32 = 0x0
    var b: UInt32 = 0x0
    Scanner.init(string: rString).scanHexInt32(&r)
    Scanner.init(string: gString).scanHexInt32(&g)
    Scanner.init(string: bString).scanHexInt32(&b)
    
    return ColorFromRGB(r: Float(r), g: Float(g), b: Float(b),a:alpha)
}
public func ColorFromRGB(r:Float,g:Float,b:Float,a:Float) -> UIColor {
    return UIColor(red: CGFloat(r/255.0), green:CGFloat(g/255.0), blue: CGFloat(b/255.0), alpha: CGFloat(a))
}

//===================渐变色===========================
extension UIView {
    public func setLayerColors(_ colors:[CGColor],_ bounds:CGRect)  {
        let layer = CAGradientLayer()
        layer.frame = bounds
        layer.colors = colors
        layer.startPoint = CGPoint(x: 0, y: 0)
        layer.endPoint = CGPoint(x: 0, y: 1)
        layer.cornerRadius = 25
        layer.masksToBounds = true
        self.layer.addSublayer(layer)
    }
}
//===================字号===========================
let kNavSize = kFont(15)
let kBigTitleSize = kFont(18)
let kBookTitleSize = kFont(21)
let k12TitleSize = kFont(12)
let k13TitleSize = kFont(13)
let k14TitleSize = kFont(14)
public func kFont(_ size:CGFloat) -> UIFont {
    return UIFont.systemFont(ofSize: size)
}
//===================获取当前vc==========================
public func currentViewController() -> UIViewController {
    var window = (UIApplication.shared.delegate?.window)!
    if window!.windowLevel !=  UIWindow.Level.normal {
        let windows = UIApplication.shared.windows
        for tmpWin in windows {
            if tmpWin.windowLevel == UIWindow.Level.normal {
                window = tmpWin
                break
            }
        }
    }
    var rootVC = (window?.rootViewController)!
    var activityVC:UIViewController!
    while true {
        if (rootVC.isKind(of: UINavigationController.self)) {
            activityVC = (rootVC as! UINavigationController).visibleViewController
        }else if(rootVC.isKind(of: UITabBarController.self)) {
            activityVC = (rootVC as! UITabBarController).selectedViewController
        }else if((rootVC.presentedViewController) != nil) {
            activityVC = rootVC.presentedViewController
        }else{
            break
        }
        if activityVC != nil {
            rootVC = activityVC
        }
    }
    return activityVC == nil ? rootVC :  activityVC
}
//===================打印信息==========================
func printLog(_ items: Any..., separator: String = " ", terminator: String = "\n"){
    #if DEBUG
    print("\(items)\n方法:\(separator)\n行号:\(terminator)");
    #endif
}


// 跳转
func PresentViewController(_ vc:UIViewController){
    vc.modalPresentationStyle = .fullScreen
    currentViewController().present(vc, animated: true)
}

